package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

/*
* 齿轮view
* */
class GearView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private var ringWidth = 0F

    private val degreeArr = floatArrayOf(0F, 60F, 120F, 180F, 240F, 300F)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_D68F73)
            style = Paint.Style.FILL
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_D68F73)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //尖角
            strokeJoin = Paint.Join.MITER
            color = context.resources.getColor(R.color.color_D68F73)
            style = Paint.Style.FILL
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        ringWidth = context.resources.getDimension(R.dimen.dp_5)
        mPaint1.strokeWidth = ringWidth
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth / 2.0F
            val cy = mHeight / 2.0F
            translate(cx, cy)
            drawCircle(0F, 0F, cx / 2, mPaint0)
            drawCircle(0F, 0F, cx - ringWidth * 3 / 2, mPaint1)
            for (degree in degreeArr) {
                save()
                rotate(degree)
                drawRect(
                    cx,
                    -ringWidth,
                    cx / 2 + ringWidth * 3 / 2,
                    ringWidth,
                    mPaint2
                )
                restore()
            }
        }
    }
}